#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>

#define MSGKEY	123456
#define MSGSZ	1024
#define MSGTYPE	3

const char message[]="Hello from C";

/* MQ message structure */
typedef struct Message{
    long    type;
    char    text[8192];
} Message;

main()
{
    int msqid;
    int msgflg = IPC_CREAT | 0666;
    key_t key;
    Message msg;
    size_t buf_length;
	/* key for MQ */
    key = 123456;
	(void) fprintf(stderr, "\nmsgget: Calling msgget(%#lx,%#o)\n", key, msgflg);
	/* try to get MQ ID*/
    if ((msqid = msgget(key, msgflg )) < 0) {
        perror("msgget");
        exit(1);
    }
    else {
		(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
    }
	/* msg type is 3 for tests */
    msg.type = 3;
    (void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
    (void) strcpy(msg.text, message);
    (void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
    buf_length = strlen(msg.text) + 1 ;
	/* send message */
    if (msgsnd(msqid, &msg, buf_length, IPC_NOWAIT) < 0) {
       printf ("%d, %d, %s, %d\n", msqid, msg.type, msg.text, buf_length);
        perror("msgsnd");
        exit(1);
    }

    else printf("Message: \"%s\" Sent\n", msg.text);
    exit(0);
}
